package com.smartcommunity.service.statistic;

import com.smartcommunity.api.peopleRegular.FrequentVisitorApi;
import com.smartcommunity.api.peopleRegular.LateAtNightPersonnelApi;
import com.smartcommunity.api.peopleRegular.LongTimeNotAppearedApi;
import com.smartcommunity.bean.model.Result;
import com.smartcommunity.dao.alarm.AlarmDao;
import com.smartcommunity.entity.statistic.*;
import com.smartcommunity.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author nieming
 * @descrition
 * @date 2020/6/8
 */
@Service
public class StatisticService {
    @Autowired
    private FrequentVisitorApi frequentVisitorApi;
    @Autowired
    private LateAtNightPersonnelApi lateAtNightPersonnelApi;
    @Autowired
    private LongTimeNotAppearedApi longTimeNotAppearedApi;
    @Autowired
    private AlarmDao alarmDao;
    @Autowired
    private AccessStatisticService accessStatisticService;


    public StatisticPeopleRegular getPeopleRegularStatistic(Integer lastTime) {
        StatisticPeopleRegular result = new StatisticPeopleRegular();
        Result<Integer> longTimeNotAppeared = longTimeNotAppearedApi.getLongTimeNotAppearedStatistics(lastTime);
        if (null != longTimeNotAppeared && null != longTimeNotAppeared.getData()) {
            result.setImportantDisappeard(longTimeNotAppeared.getData());
        }
        Result<Integer> lateAtNight = lateAtNightPersonnelApi.getLateAtNightStatistics();
        if (null != lateAtNight && null != lateAtNight.getData()) {
            result.setImportantNightBusy(lateAtNight.getData());
        }
        Result<Integer> unRegisterBusy = frequentVisitorApi.getUnRegisterBusyStatistics();
        if (null != unRegisterBusy && null != unRegisterBusy.getData()) {
            result.setUnRegisterBusy(unRegisterBusy.getData());
        }
        return result;
    }

    public List<StatisticExceptionAlarmResp> geAlarmStatistic() {
        List<StatisticExceptionAlarmResp> weekDates = getStatisDate();
        for (StatisticExceptionAlarmResp statisticExceptionAlarmResp : weekDates) {
            Integer alarmCount = alarmDao.queryCountByTime(statisticExceptionAlarmResp.getWeekStart(), statisticExceptionAlarmResp.getWeekEnd());
            statisticExceptionAlarmResp.setAlarmCount(alarmCount);
        }
        return weekDates;
    }

    public List<AccessStatisticDataResp> getFaceAccessStatistic(String accessType) {
        List<AccessStatisticDataResp> realFaceList = getFaceAccessStatisticDate();
        for (AccessStatisticDataResp accessStatisticDataResp : realFaceList) {
            accessStatisticDataResp.setAccessType(accessType);
            Long count = accessStatisticService.queryFaceCount(accessStatisticDataResp);
            accessStatisticDataResp.setAccessCount(count == null ? 0L : count);
        }
        return realFaceList;
    }

    public List<StatisticExceptionAlarmResp> getStatisDate() {
        Date nowDate = new Date();
        List<StatisticExceptionAlarmResp> weekDates = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Date tempDate = DateUtil.addDays(nowDate, i * -7);
            StatisticExceptionAlarmResp statisticExceptionAlarmResp = new StatisticExceptionAlarmResp();
            statisticExceptionAlarmResp.setWeekStart(DateUtil.toString(DateUtil.getWeekStart(tempDate), DateUtil.DATE_FULL_FORMAT_START));
            statisticExceptionAlarmResp.setWeekEnd(DateUtil.toString(DateUtil.getWeekEnd(tempDate), DateUtil.DATE_FULL_FORMAT_END));
            weekDates.add(statisticExceptionAlarmResp);
        }
        return weekDates;
    }

    /**
     * 获取当前时间向后推24小时的时间
     *
     * @return
     */
    public List<AccessStatisticDataResp> getFaceAccessStatisticDate() {
        Date nowDate = new Date();
        List<AccessStatisticDataResp> weekDates = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            Date tempDate = DateUtil.addHour(nowDate, -i);
//            String tempDate = DateUtil.toString(DateUtil.addHour(nowDate, -i), DateUtil.DATE_HOUR_FORMAT);
            AccessStatisticDataResp accessStatisticDataResp = new AccessStatisticDataResp();
            accessStatisticDataResp.setAccessDay(DateUtil.toString(tempDate, DateUtil.DATE_SIMPLE_FORMAT));
            accessStatisticDataResp.setAccessHour(DateUtil.toString(tempDate, DateUtil.DATE_HOUR_ONLY));
            weekDates.add(accessStatisticDataResp);
        }
        return weekDates;
    }

}
